package com.jlb.demo.exception;

/**
 * 业务异常类 - 演示自定义异常
 * 用于表示业务逻辑错误，如用户不存在、订单已支付等
 */
public class BusinessException extends RuntimeException {
    
    /**
     * 错误码
     */
    private Integer code;
    
    /**
     * 错误消息
     */
    private String message;
    
    /**
     * 构造方法
     * @param code 错误码
     * @param message 错误消息
     */
    public BusinessException(Integer code, String message) {
        super(message);
        this.code = code;
        this.message = message;
    }
    
    /**
     * 构造方法
     * @param code 错误码
     * @param message 错误消息
     * @param cause 原因
     */
    public BusinessException(Integer code, String message, Throwable cause) {
        super(message, cause);
        this.code = code;
        this.message = message;
    }
    
    /**
     * 获取错误码
     * @return 错误码
     */
    public Integer getCode() {
        return code;
    }
    
    /**
     * 设置错误码
     * @param code 错误码
     */
    public void setCode(Integer code) {
        this.code = code;
    }
    
    @Override
    public String getMessage() {
        return message;
    }
    
    /**
     * 设置错误消息
     * @param message 错误消息
     */
    public void setMessage(String message) {
        this.message = message;
    }
    
    /**
     * 获取完整的错误信息
     * @return 完整错误信息
     */
    public String getFullMessage() {
        return String.format("[错误码:%d] %s", code, message);
    }
    
    /**
     * 预定义的错误码
     */
    public static class ErrorCode {
        public static final Integer USER_NOT_FOUND = 1001;
        public static final Integer ORDER_NOT_FOUND = 1002;
        public static final Integer ORDER_ALREADY_PAID = 1003;
        public static final Integer INSUFFICIENT_BALANCE = 1004;
        public static final Integer INVALID_ORDER_STATUS = 1005;
        public static final Integer PRODUCT_NOT_FOUND = 1006;
        public static final Integer INSUFFICIENT_STOCK = 1007;
        public static final Integer INVALID_PARAMETER = 1008;
    }
} 